/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditESchedulerPanel.java
 *
 * Created on Feb 6, 2012, 6:10:42 PM
 */
package ui.panels;

import entities.Shift;
import entities.ViewScheduler;
import java.awt.Color;
import java.util.Date;
import javax.swing.JOptionPane;
import manager.DepartmentHeadDriver;
import manager.GenericDriver;
import model.ViewShiftComboBoxModel;
import ui.windows.RemedySchedulerFrame;

/**
 *
 * @author viev
 */
public class EditESchedulerPanel extends javax.swing.JPanel {

    /** Creates new form EditESchedulerPanel */
    public EditESchedulerPanel(RemedySchedulerFrame mainFrame, ESchedulerManagementPanel parentPanel) {
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        driver = (DepartmentHeadDriver) mainFrame.getDriver();
        genDriver = mainFrame.getGenericDriver();
        initComponents();
        includeModel();
    }
    
    private void includeModel() {
        model = new ViewShiftComboBoxModel(genDriver.showShift());
        cbShifts.setModel(model);
        cbShifts.setSelectedItem(null);
        checkShift();
        checkDate();
    }
    
    private void updateRecord() {
        checkShift();
        checkDate();
        if(checkShift && checkDate) {
            Date endDate = dcEndDate.getDate();
            selectedRecord.setEndDate(endDate);   
            selectedRecord.setShiftID(cbShifts.getSelectedIndex() + 1);
            boolean returnVal = parentPanel.updateScheduler(selectedRecord);
            if(returnVal) {
                close();
                JOptionPane.showMessageDialog(null, "Updated");
            } else {
                JOptionPane.showMessageDialog(null, "Cannot update !", "Error", JOptionPane.WARNING_MESSAGE);
            }
        }
    }
    
    private void checkShift() {
        Shift shift = (Shift) cbShifts.getSelectedItem();
        if(shift != null) {
            lbCheckShift.setText("");
            checkShift = true;
        } else {
            lbCheckShift.setText("enter shift...");
            lbCheckShift.setForeground(Color.blue);
            checkShift = false;
        }
    }
    
    private void checkDate() {
        Date startDate = dcStartDate.getDate();
        Date endDate = dcEndDate.getDate();
        if(endDate != null && endDate.before(startDate)) {
            lbCheckDate.setText("invalid !");
            lbCheckDate.setForeground(Color.red);
            checkDate = false;
        } else {
            lbCheckDate.setText("");
            checkDate = true;
        }
    }
    
    public void setSelectedRecord(ViewScheduler record) {
        this.selectedRecord = record;
        if(selectedRecord != null) {
            lbID.setText(selectedRecord.getEmployeeID()+"");
            lbDep.setText(selectedRecord.getDepartmentName());
            cbShifts.setSelectedIndex(selectedRecord.getShiftID() - 1);
            dcStartDate.setDate(selectedRecord.getStartDate());
            dcEndDate.setDate(selectedRecord.getEndDate());
        } else {
            lbID.setText("");
            lbDep.setText("");
            cbShifts.setSelectedItem(null);
            dcStartDate.setDate(null);
            dcEndDate.setDate(null);
        }
    }

    private void clear() {
        lbID.setText("");
        lbDep.setText("");
        lbCheckShift.setText("");
        dcStartDate.setDate(null);
        dcEndDate.setDate(null);
        lbCheckDate.setText("");
        cbShifts.setSelectedItem(null);
        selectedRecord = null;
    }
    
    private void close() {
        clear();
        parentPanel.closeTarget();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private ESchedulerManagementPanel parentPanel;
    private DepartmentHeadDriver driver;
    private GenericDriver genDriver;
    private ViewShiftComboBoxModel model;
    private ViewScheduler selectedRecord;
    private boolean checkShift;
    private boolean checkDate;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbClose = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lbID = new javax.swing.JLabel();
        lbDep = new javax.swing.JLabel();
        cbShifts = new javax.swing.JComboBox();
        dcStartDate = new com.toedter.calendar.JDateChooser();
        dcEndDate = new com.toedter.calendar.JDateChooser();
        lbCheckShift = new javax.swing.JLabel();
        lbCheckDate = new javax.swing.JLabel();
        bUpdate = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        lbClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/close_20.png"))); // NOI18N
        lbClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbCloseMouseClicked(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Employee's Scheduler Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        jLabel2.setText("ID:");

        jLabel3.setText("Department:");

        jLabel4.setText("Shift:");

        jLabel5.setText("Start Date:");

        jLabel6.setText("End Date:");

        lbID.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbID.setText("value");

        lbDep.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbDep.setText("value");

        cbShifts.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbShifts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbShiftsActionPerformed(evt);
            }
        });

        dcStartDate.setEnabled(false);

        lbCheckShift.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckShift.setText("value");

        lbCheckDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckDate.setText("value");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dcEndDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbID)
                    .addComponent(lbDep)
                    .addComponent(cbShifts, 0, 110, Short.MAX_VALUE)
                    .addComponent(dcStartDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbCheckDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbCheckShift, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbDep))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cbShifts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckShift))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(dcStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(dcEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lbCheckDate))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        bUpdate.setText("Update");
        bUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(657, Short.MAX_VALUE)
                .addComponent(lbClose)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(bUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(178, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbClose))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bUpdate)
                .addContainerGap(130, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void cbShiftsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbShiftsActionPerformed
    checkShift();
}//GEN-LAST:event_cbShiftsActionPerformed

private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateActionPerformed
    updateRecord();
}//GEN-LAST:event_bUpdateActionPerformed

private void lbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbCloseMouseClicked
    close();
}//GEN-LAST:event_lbCloseMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bUpdate;
    private javax.swing.JComboBox cbShifts;
    private com.toedter.calendar.JDateChooser dcEndDate;
    private com.toedter.calendar.JDateChooser dcStartDate;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbCheckDate;
    private javax.swing.JLabel lbCheckShift;
    private javax.swing.JLabel lbClose;
    private javax.swing.JLabel lbDep;
    private javax.swing.JLabel lbID;
    // End of variables declaration//GEN-END:variables
}
